#include <iostream>


using namespace std;

const int N = 1e6 + 10;
int a[N];
int h;
int id;


int main()
{
	//处理输入
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	cin >> h;

	for (int i = h; i; i = a[i])
	{
		cout << i << " ";	
	}
	

	return 0;

}
